namespace DotNetNuke.HttpModules.Config
{
    using DotNetNuke.Common.Utilities;
    using System;
    using System.IO;
    using System.Web;
    using System.Web.Caching;
    using System.Xml.Serialization;

    [Serializable, XmlRoot("RewriterConfig")]
    public class RewriterConfiguration
    {
        private RewriterRuleCollection _rules;

        public static RewriterConfiguration GetConfig()
        {
            if (DataCache.GetCache("RewriterConfig") == null)
            {
                string filePath = HttpRuntime.AppDomainAppPath + "SiteUrls.config";
                XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration));
                FileStream fileReader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                StreamReader reader = new StreamReader(fileReader);
                RewriterConfiguration config = (RewriterConfiguration) ser.Deserialize(reader);
                reader.Close();
                fileReader.Close();
                if (!File.Exists(filePath))
                {
                    return config;
                }
                CacheDependency dep = new CacheDependency(filePath);
                DataCache.SetCache("RewriterConfig", config, dep);
            }
            return (RewriterConfiguration) DataCache.GetCache("RewriterConfig");
        }

        public RewriterRuleCollection Rules
        {
            get
            {
                return this._rules;
            }
            set
            {
                this._rules = value;
            }
        }
    }
}

